package com.itany.corejava.code13_jdbc;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author MissChen
 * @version 1.0
 * @date 2023 年12月12日 16:04
 */


public class Test08_多表保存 {
    public static void main(String[] args) {
        try {
            // int id = insertDeptReturnPrimaryKey(new Dept("生产部", "生产机器"));

            //使用外键字段关联
            // insertEmp(new Emp("韩智恩", 8000.0, id));
            // insertEmp(new Emp("闫书博", 9000.0, id));
            // insertEmp(new Emp("马凡娟", 10000.0, id));

            Dept dept=new Dept("生产部2","生产粮食");
            int id=insertDeptReturnPrimaryKey(dept);
            System.out.println("部门添加成功了");

            //使用对象的属性进行关联
            dept.setId(id);
            Emp e1=new Emp("xiaohua1",7999.0,dept);
            Emp e2=new Emp("xiaohua2",8999.0,dept);
            Emp e3=new Emp("xiaohua3",9999.0,dept);
            insertEmp(e1);
            insertEmp(e2);
            insertEmp(e3);
            System.out.println("添加员工成功");



        } catch (DataAccessException e) {
            e.printStackTrace();
        }
    }

    public static Integer insertDeptReturnPrimaryKey(Dept dept) throws DataAccessException {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        Integer id = null;
        try {
            conn = JDBCUtil.getConnection();
            String sql = new StringBuffer()
                    .append(" insert into ")
                    .append(" t_dept ")
                    .append(" (name,remark) ")
                    .append(" values (?,?) ")
                    .toString();
            ps = conn.prepareStatement(sql, PreparedStatement.RETURN_GENERATED_KEYS);
            ps.setString(1, dept.getName());
            ps.setString(2, dept.getRemark());
            ps.executeUpdate();
            rs = ps.getGeneratedKeys();
            while (rs.next()) {
                id = rs.getInt(1);
            }

        } catch (DataAccessException e) {
            e.printStackTrace();
            throw e;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new DataAccessException("数据库访问失败");
        } finally {
            JDBCUtil.close(conn, ps, rs);
        }

        return id;

    }

    public static void insertEmp(Emp emp) throws DataAccessException {
        Connection conn = null;
        PreparedStatement ps = null;


        try {
            conn = JDBCUtil.getConnection();
            String sql = new StringBuffer()
                    .append(" insert into ")
                    .append(" t_emp ")
                    .append(" (name,salary,dept_id) ")
                    .append(" values (?,?,?) ")
                    .toString();
            ps = conn.prepareStatement(sql);
            ps.setString(1, emp.getName());
            ps.setDouble(2, emp.getSalary());

            //使用外键字段关联
            // ps.setInt(3, emp.getDeptId());

            //使用对象关联(通过对象的属性关联)
            ps.setInt(3,emp.getDept().getId());

            int row = ps.executeUpdate();
            System.out.println(row + " rows affected ");


        } catch (DataAccessException e) {
            e.printStackTrace();
            throw e;
        } catch (SQLException e) {
            e.printStackTrace();
            throw new DataAccessException("数据库访问失败");
        } finally {
            JDBCUtil.close(conn, ps, null);
        }


    }


}
